package com.example.nkj.sensor;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.support.v7.app.AppCompatActivity;
import java.util.List;


public class sensor extends AppCompatActivity implements SensorEventListener
{
    private SensorManager sm = null;
    private TextView View1 = null;
    private TextView View2 = null;
    private TextView View3 = null;
    private TextView View4 = null;
    private TextView View5 = null;
    private TextView View6 = null;
    private TextView View7 = null;
    private TextView View8 = null;
    private TextView View9 = null;
    private TextView View10 = null;
    private TextView View11 = null;
    private TextView View12 = null;
    private TextView View13 = null;
    private TextView View14 = null;
    private TextView View15 = null;
    private TextView View16 = null;
    private TextView View17 = null;
    private TextView View18 = null;
    private TextView View19 = null;
    private TextView View20 = null;
    private TextView View21 = null;
    private TextView View22 = null;
    private TextView View23 = null;

    private static final String TAG = "sensor";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sensor);
        sm = (SensorManager) getSystemService(SENSOR_SERVICE);

        View1 = (TextView) findViewById(R.id.edt1);
        View2 = (TextView) findViewById(R.id.edt2);
        View3 = (TextView) findViewById(R.id.edt3);
        View4 = (TextView) findViewById(R.id.edt4);
        View5 = (TextView) findViewById(R.id.edt5);
        View6 = (TextView) findViewById(R.id.edt6);
        View7 = (TextView) findViewById(R.id.edt7);
        View8 = (TextView) findViewById(R.id.edt8);
        View9 = (TextView) findViewById(R.id.edt9);
        View10 = (TextView) findViewById(R.id.edt10);
        View11 = (TextView) findViewById(R.id.edt11);
        View12 = (TextView) findViewById(R.id.edt12);
        View13 = (TextView) findViewById(R.id.edt13);
        View14 = (TextView) findViewById(R.id.edt14);
        View15 = (TextView) findViewById(R.id.edt15);
        View16 = (TextView) findViewById(R.id.edt16);
        View17 = (TextView) findViewById(R.id.edt17);
        View18 = (TextView) findViewById(R.id.edt18);
        View19 = (TextView) findViewById(R.id.edt19);
        View20 = (TextView) findViewById(R.id.edt20);
        View21 = (TextView) findViewById(R.id.edt21);
        View22 = (TextView) findViewById(R.id.edt22);
        View23 = (TextView) findViewById(R.id.edt23);

        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PRESSURE), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_TEMPERATURE), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_GRAVITY), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION), SensorManager.SENSOR_DELAY_NORMAL);
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR), SensorManager.SENSOR_DELAY_NORMAL);

    }

    public void onSensorChanged(SensorEvent event)
    {
        synchronized (this)
        {
            //String str = "X：" + event.values[0] + "，Y：" + event.values[1] + "，Z：" + event.values[2];
            String str = String.format("X:% 11.5f, Y:% 11.5f, Z:% 11.5f",event.values[0],event.values[1],event.values[2]);
            switch (event.sensor.getType())
            {
                case Sensor.TYPE_ACCELEROMETER:
                    View1.setText("加    速    度：" + str);
                    break;
                case Sensor.TYPE_MAGNETIC_FIELD:
                    View2.setText("磁            场：" + str);
                    break;
                case Sensor.TYPE_ORIENTATION:
                    View3.setText("方            位：" + str);
                    break;
                case Sensor.TYPE_GYROSCOPE:
                    View4.setText("陀    螺    仪：" + str);
                    break;
                case Sensor.TYPE_LIGHT:
                    View5.setText("光            线：" + str);
                    break;
                case Sensor.TYPE_PRESSURE:
                    View6.setText("压            力：" + str);
                    break;
                case Sensor.TYPE_TEMPERATURE:
                    View7.setText("温            度：" + str);
                    break;
                case Sensor.TYPE_PROXIMITY:
                    View8.setText("距            离：" + str);
                    break;
                case Sensor.TYPE_GRAVITY:
                    View9.setText("重            力：" + str);
                    break;
                case Sensor.TYPE_LINEAR_ACCELERATION:
                    View10.setText("线性加速度:" + str);
                    break;
                case Sensor.TYPE_ROTATION_VECTOR:
                    View11.setText("旋 转  矢 量：" + str);
                    break;
                default:
                    View12.setText("NORMAL：" + str);
                    break;
            }
        }
    }

    public void onAccuracyChanged(Sensor sensor, int accuracy)
    {
        Log.d(TAG,"onAccuracyChanged: " + sensor + ", accuracy: " + accuracy);
    }

    @Override
    protected void onResume()
    {
        // 得到设置支持的所有传感器的List
        int i= 0;
        List<Sensor> sensorList;
        sensorList = sm.getSensorList(Sensor.TYPE_ALL);

        for (Sensor sensor : sensorList)
        {
            //Log.d(TAG, "onResume: "+sensor.getName());
            if(i == 0)
                View13.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 1)
                View14.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 2)
                View15.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 3)
                View16.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 4)
                View17.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 5)
                View18.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 6)
                View19.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 7)
                View20.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 8)
                View21.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 9)
                View22.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());
            else if(i == 10)
                View23.setText(sensor.getType()+","+sensor.getName()+","+sensor.getVersion()+","+sensor.getVendor()+","+sensor.getMaximumRange());

            i ++;
        }
        super.onResume();
    }

    @Override
    protected void onStop()
    {
        sm.unregisterListener(this);
        super.onStop();
    }
}
